var user = [
  { user: "100000", pass: "123456" },
  { user: "100002", pass: "123456" },
  { user: "100003", pass: "123456" },
  { user: "100004", pass: "123456" },
  { user: "100005", pass: "123456" },
  { user: "100006", pass: "123456" },
  { user: "100007", pass: "123456" },
  { user: "100008", pass: "123456" },
  { user: "100009", pass: "123456" },
  { user: "100010", pass: "123456" },
  { user: "100011", pass: "123456" },
];
var a = 1;
var b = 2;

localStorage.setItem("user", JSON.stringify(user));
localStorage.setItem("a", a);
localStorage.setItem("b", b);
var oForm = document.querySelector("form");
var oInp = document.querySelectorAll("input");
var oUser = document.querySelector("#user");
var oPass = document.querySelector("#pass");

oForm.addEventListener("submit", login);
function login() {
  var user = oUser.value;
  var pass = oPass.value;
  //正则  是否为空 账号密码的格式是否正确
  //如果正则没过
  //提示问题
  //event.preventDefault();
  //如果正则过了  }
  //验证账户是否存在
  var data = JSON.parse(localStorage.getItem("user"));
  var index = test(data, user, pass);
  if (index != -1) {
    //存在
    alert("登陆成功");
    //存用户名
    localStorage.setItem("username", data[index].name);
  } else {
    //不存在
    alert("密码错误");
    event.preventDefault();
  }
}
oInp[0].onblur = function () {
  var str = this.value;
  var reg = /^\d{6}$/;
  if (str.trim() == "") {
    this.parentNode.nextElementSibling.innerHTML = "内容不能为空";
  } else if (reg.test(str) == false) {
    this.parentNode.nextElementSibling.innerHTML = "请输入正确的账号";
  } else if (reg.test(str) == true) {
    this.parentNode.nextElementSibling.innerHTML = "√";
  }
};

oInp[1].onblur = function () {
  var str = this.value;
  var reg = /^\d{6}$/;
  if (str.trim() == "") {
    this.parentNode.nextElementSibling.innerHTML = "内容不能为空";
  } else if (reg.test(str) == false) {
    this.parentNode.nextElementSibling.innerHTML = "密码格式不正确";
  } else if (reg.test(str) == true) {
    this.parentNode.nextElementSibling.innerHTML = "√";
  }
};
function test(userData, username, password) {
  for (var i = 0; i < userData.length; i++) {
    if (userData[i].user == username && userData[i].pass == password) {
      return i;
    }
  }
  return -1;
}
